
import java.net.*;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import javanetworking.packet;

class UDPClient2 {

    public byte[] returnFunction(File fi, long offset) throws FileNotFoundException, IOException {
        long fiLength = fi.length();
        long size = 1024;
        if (fi.length() - offset < 1024) {
            size = fi.length() - offset;
        }
        System.out.println(size + "");
        byte[] readByte = new byte[(int) size];
        RandomAccessFile rd = new RandomAccessFile(fi, "r");
        rd.seek(offset);
        int kq = rd.read(readByte);
        if (kq < 0) {
            return null;
        }
        return readByte;
    }

    public static void main(String[] args) {
        try {
//			byte[] sentBuf = new byte[100];
//			String sentMessage = "Hello, there !";
//			System.out.println(" Sent : " + sentMessage);
//			
//			InetAddress sentHost = InetAddress.getByName("localhost");
            DatagramSocket utpSocket = new DatagramSocket(3500);

//			sentBuf = sentMessage.getBytes("ascii");
//			DatagramPacket sentPacket = new DatagramPacket(sentBuf, sentBuf.length, sentHost, 3400);
//			
//			utpSocket.send(sentPacket);
            List<byte[]> block = new ArrayList<>();

            File fi = new File("abc.exe");
            FileOutputStream fos = new FileOutputStream(fi);


            ByteArrayInputStream bis = null;
            ObjectInputStream ois = null;
            long dem=0;
//            for (int i = 0; i < 3; i++) {
                byte[] receivedBuf = new byte[1024];
                byte[] receivedBuf2 = new byte[1024];
                packet q=new packet();
                q.setCode(1);
                q.setOffset(dem);
                receivedBuf=q.toByteArray();
                InetAddress add = InetAddress.getByName("192.168.9.101");
                DatagramPacket receivedPacket = new DatagramPacket(receivedBuf, receivedBuf.length,add,8888);
                utpSocket.send(receivedPacket);
                System.out.println("Sent !!!"+dem);
                
                DatagramPacket receivedPacket2 = new DatagramPacket(receivedBuf, receivedBuf.length);
                utpSocket.receive(receivedPacket2);
                bis = new ByteArrayInputStream(receivedPacket2.getData());
                ois = new ObjectInputStream(bis);

                packet q2 = (packet)ois.readObject();
                byte[] b2 = new byte[q2.getData().length];
                System.out.println(0 + ". Da Nhan: " + q2.getLength());
                dem+=q2.getData().length;
                b2 = q2.getData();
                block.add(b2);
               
                
              
//            }
            System.out.println("Chuan bi ghep file");
            ByteArrayOutputStream os = new ByteArrayOutputStream();
            for (byte[] b : block) {
                os.write(b, 0, b.length); // noi cac byte[] lai voi nhau bang os
            }

            byte[] oByte = os.toByteArray();

            os.close();
            fos.write(oByte);
            fos.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
